package com.loong.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.entity.Article;
import com.loong.entity.Category;
import com.loong.entity.Word;
import com.loong.entity.WordBook;
import com.loong.entity.dto.WordBookDTO;
import com.loong.entity.vo.WordBookPage;
import com.loong.entity.vo.WordBookVO;
import com.loong.service.impl.ArticleServiceImpl;
import com.loong.service.impl.CategoryServiceImpl;
import com.loong.service.impl.WordBookServiceImpl;
import com.loong.service.impl.WordServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author loong
 * @since 2024-12-08
 */
@RestController
@RequestMapping("/wordBook")
public class WordBookController {

    @Autowired
    private WordServiceImpl wordService;

    @Autowired
    private WordBookServiceImpl wordBookService;


    @Autowired
    private CategoryServiceImpl categoryService;

    @Autowired
    private ArticleServiceImpl articleService;

    @GetMapping("/getWordBooks")
    public List<List<WordBook>> getWordbooks() {
        List<WordBook> wordBooks = wordBookService.list();
        List<List<WordBook>> result=wordBookService.category(wordBooks);
        // 对words进行分组，每组4个wordbook，其中第一组只分配三个
        return result;
    }

    /**
     * 分页查询单词本
     * @param pageNumber
     * @param pageSize
     * @param keyWord
     * @param sortManner
     * @return
     */
    @GetMapping("/searchWordBookByPage")
    public WordBookPage searchWordBookByPage(@RequestParam(value = "pageNumber",required = false) Integer pageNumber,@RequestParam(value = "pageSize",required = true) Integer pageSize,@RequestParam(value = "keyWord",required = false) String keyWord,@RequestParam(value = "sortManner",required = false) String sortManner){
        // 初始化查询参数
        if(pageNumber==null){
            pageNumber=0;
        }

        if(sortManner==null){
            sortManner="按照时间排序";
        }
        LambdaQueryWrapper <WordBook> queryWrapper=new LambdaQueryWrapper<>(); // 查询器
        // 构建查询器
        if(keyWord!=null){
            queryWrapper.like(WordBook::getName,keyWord).le(WordBook::getCreateTime,new Date())
                        .or().like(WordBook::getCategoryName,keyWord);
        }
        if(sortManner.equals("按照时间排序")){
            queryWrapper.orderByDesc(WordBook::getCreateTime);
        }else if(sortManner.equals("按照单词数量排序")){
            queryWrapper.orderByDesc(WordBook::getWordCount);
        }else if(sortManner.equals("按照重要性排序")){
            queryWrapper.orderByDesc(WordBook::getImportance);
        }else if(sortManner.equals("按照复习时间排序")){
            queryWrapper.orderByDesc(WordBook::getLastReviewTime);
        }else{
            queryWrapper.orderByDesc(WordBook::getCreateTime);
        }
        // 分页查询
        Page<WordBook> page=new Page<>(pageNumber,pageSize);
        List<WordBook> wordBooks = wordBookService.list(page, queryWrapper);
        // 返回查询的结果
        long wordBookPages=page.getPages();
        long totalWordBookNumber=page.getTotal();
        List<List<WordBook>> lists = wordBookService.category(wordBooks);
        WordBookPage wordBookPage = new WordBookPage();
        wordBookPage.setWordBookPages(wordBookPages);
        wordBookPage.setTotalWordBookNumber(totalWordBookNumber);
        wordBookPage.setWordBooks(lists);
        return wordBookPage;
    }

    @GetMapping("/getCurrentWordBook")
    public WordBook getCurrentWordbook() {
        List<WordBook> wordBooks = wordBookService.list();
        return wordBooks.get(wordBooks.size() - 1);
    }

    @PostMapping("/create")
    public boolean createWordBook(@RequestBody WordBookDTO wordBookDTO) {
        // 查询文章id和分类id是否存在，不存在的时候创建对应分类
        WordBook wordBook = wordBookDTO.getWordBook();
        Category category = wordBookDTO.getCategory();
        Article article = wordBookDTO.getArticle();
        if (article.getId() == 0) {
            // 创建对应的文章分类
            articleService.save(article);
        }

        if (category.getId() == 0) {
            categoryService.save(category);
        }

        wordBook.setArticleId(article.getId());
        wordBook.setCategoryId(category.getId());
        wordBook.setCategoryName(category.getName());
        wordBookService.save(wordBook);
        return true;
    }
}
